/*数据加载工厂函数*/
$(function(){
    //初始化数据(初始加载出行方式、初始加载行程列表)
    initTTripMode();
    //默认查询所有
    initTSchedule(0);
});


function initTSchedule(sel){
    $.ajax({
        url : "http://localhost:8080/TScheduleServlet",
        type : "get",
        data : { "tripModeId" : sel },     //前端参数传递到后端程序的入口
        dataType : "json",
        success:function (res) {
            console.log(res.tSchedules);
            // 获取出行方式数据
            // $.each(tTripModes,function(){});
            let tableStr = "";
            let tripModeIds = new Array("公共交通","高铁","飞机","公司派车","自驾","其他");
            //行程状态
            let tripStatus = [];
            tripStatus.push("未出行");
            tripStatus.push("已取消");
            //线程显示操作
            let tripStatusOper = ["取消行程","已取消"];
            $.each(res.tSchedules,function(i){
                let tSchedule = res.tSchedules[i];
                tableStr += ` <tr>
                                <td>${tSchedule.userName}</td>
                                <td>${tSchedule.duty}</td>
                                <td>${tripModeIds[tSchedule.tripModeId - 1]}</td>
                                <td>${tSchedule.departTime}</td>
                                <td>${tripStatus[tSchedule.status]}</td>
                                <td>
                                    <a href="javascript:void(0);" 
                                    onclick="cancelTrip(${tSchedule.status},${tSchedule.id});">${tripStatusOper[tSchedule.status]}</a>
                                    <a href="javascript:void(0);" onclick="delTrip(${tSchedule.id});">删除</a>    
                                </td>
                            </tr> `;
            });
            $("tbody").html(tableStr);
        },
        error:function () {
            console.log("initTTripMode  err");
        }
    });
}

//是否取消行程
function cancelTrip(status,tScheduleId){
    console.log("status:" + status );
    console.log("tScheduleId:" + tScheduleId );
    $.ajax({
        url : "http://localhost:8080/TScheduleEditServlet",
        type : "post",
        data : {"status":status,"tScheduleId":tScheduleId},
        success : function (res){
            console.log(typeof(res));
            if(res){
                alert("行程取消成功");
              //  window.location.href = "http://localhost:8080/employee/pageHome.html";
                initTSchedule(0);
            }else{
                alert("行程取消失败");
            }
        },
        error:function () {
            console.log("cancelTrip err");
        }
    });
}

//删除行程
function delTrip(tScheduleId){
    console.log("tScheduleId:" + tScheduleId);
    $.ajax({
        url : "http://localhost:8080/TScheduleDeleteServlet",
        type : "post",
        data : {"tScheduleId":tScheduleId},
        success : function (res){
            console.log(res + ",res返回的类型:" + typeof(res));
            if(res){
                alert("删除成功");
              //  window.location.href = "http://localhost:8080/employee/pageHome.html";
                initTSchedule(0);
            }else{
                alert("删除失败");
            }
        },
        error:function () {
            console.log("delTrip err");
        }
    });
}


//条件筛选
function search(){
   // 获取下拉框选择的value
   let sel = $("#sel").val();
   console.log("sel:" + sel);
   //调用查询行程列表函数
   initTSchedule(sel);
}

//跳转添加页面  saveTravel.html
/*function addTravel(){
    window.local.href = "http://localhost:8080/employee/addTravel.html";
}*/
